package com.zlq.cn.service.movie;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlq.cn.api.ResponseException;
import com.zlq.cn.bootstrap.SignApiBootstrap;
import com.zlq.cn.dto.param.MovieLoginParam;
import com.zlq.cn.dto.param.MovieRegisterParam;
import com.zlq.cn.entity.SmsRecord;
import com.zlq.cn.entity.User;
import com.zlq.cn.mapper.un.UserMapper;
import com.zlq.cn.service.un.SmsRecordService;
import com.zlq.cn.service.un.UserService;
import com.zlq.cn.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhoucl
 * @since 2020-06-08
 */
@Service
public class MovieUserService extends ServiceImpl<UserMapper, User> {
    private static final String PROJECT = "MOVIE";
    private static final String[] AVATAR = ("http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/3eb531d8e92b3289/bf3e253c12021502.jpg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/4e63b966a0c6ea1c/d66b1632bced7318.jpeg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/5c1fac18e64fd875/63638560fa222339.jpeg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/4c9eb60a62f9fb71/cc7a2998949112ae.jpeg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/ff37957c17c5c28e/64bdc0ef8422955f.jpeg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/8b08694852e773ae/3a7e88e6240a01a6.jpeg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/067e0c33d634e487/4c81b1520dbe9937.jpg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/30f25ec3a5671391/eb10f91fb6aefdf2.jpeg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/8ed5425884cd3028/0930f01efc64e385.jpeg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/fa7bbb84d619ed1a/a024fb7b169b168c.jpeg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/2d06f425b500c27b/523550393f598fb3.jpeg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/01ff620f60c88e0a/3f1357fb83ebf4b3.jpg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/37d8435a373f382e/3a3a3bacf383aa0e.jpeg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/de7d32c8b21df84e/bd9c375da288600a.jpeg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/31d9f7968a14b51c/7d7bfcb05e0231e8.jpeg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/4584ad05a33719e9/d1475d1c711474a5.jpeg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/728e4e3429c2006a/a1804321f3be67a4.jpeg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/b7c275a83d17230c/282b9d26260e1dbb.jpeg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/8898d143efc817c1/af2c129a27dd0c7c.png," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/7726f29be86a50e0/f2238a4fb80fbaa7.jpeg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/cd6349919bccbaec/88cc54c6d691152c.jpeg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/c56c40bf693701ad/5dd91018abfeabf1.jpeg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/5928b7c233e9bd17/6a6b4e9c9785343f.jpeg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/fb6df890a360bbf3/25285145b01e0cc5.jpeg," +
            "http://mifeng-android.oss-cn-hangzhou.aliyuncs.com/images/20230426/4ceeefb684ee7e55/b8c5d0f2ad0fd830.jpeg")
            .split(",");
    @Resource
    private SmsRecordService smsRecordService;

    @Resource
    private UserService userService;

    public User loginIos(MovieLoginParam param) {
        User user = super.lambdaQuery()
                .eq(User::getIosToken, param.getIosToken())
                .eq(User::getProject, PROJECT)
                .one();
        if (user == null) {
            user = new User()
                    .setProject(PROJECT)
                    .setAvatar(AVATAR[RandomUtil.randomInt(AVATAR.length)])
                    .setIsDisable(0)
                    .setVipLevel(0)
                    .setCreateTime(LocalDateTime.now())
                    .setUpdateTime(LocalDateTime.now())
                    .setNewCreate(true);
            super.save(user);
        }
        user.setToken(StringUtils.createToken());
        baseMapper.updateUserToken(user);
        userService.setUserTokenUserId(user);
        return user;
    }

    public User login(MovieLoginParam param) {
        User user = super.lambdaQuery()
                .eq(User::getPhone, param.getPhone())
                .eq(User::getPassword, param.getPassword())
                .eq(User::getProject, PROJECT)
                .one();
        user.setToken(StringUtils.createToken());
        baseMapper.updateUserToken(user);
        userService.setUserTokenUserId(user);
        return user;
    }

    public boolean register(MovieRegisterParam param) {
        if (!smsRecordService.findCheckSmsVcode(param.getPhone(), param.getVcode(), SmsRecord.SMS_TYPE_LOGIN)) {
            ResponseException.resultApiError("验证码错误");
        }
        Integer count = super.lambdaQuery()
                .eq(User::getPhone, param.getPhone())
                .eq(User::getProject, PROJECT)
                .last(" limit 1")
                .count();
        Assert.state(count == 0, "手机号已经注册");
        User user = new User()
                .setToken(StringUtils.createToken())
                .setPhone(param.getPhone())
                .setProject(PROJECT)
                .setPassword(param.getPassword())
                .setIsDisable(0)
                .setVipLevel(0)
                .setCreateTime(LocalDateTime.now())
                .setUpdateTime(LocalDateTime.now())
                .setNewCreate(true);
        super.save(user);
        return Boolean.TRUE;
    }

    public Boolean logOff() {
        User user = SignApiBootstrap.requestUser();
        super.removeById(user.getId());
        userService.clearUserCache(user);
        return Boolean.TRUE;
    }
}
